<?php

namespace Mouta\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    public function indexAction($name)
    {
        return $this->render('AdminBundle:Default:index.html.twig', array('name' => $name));
    }
    
    public function estadisticasAction()
    {
        $estadisticas = $this->getDoctrine()->getManager()->getRepository('AmistateBundle:Usuario')->getEstadisticas();
        return $this->render('AdminBundle:Default:estadisticas.html.twig', array('estadisticas' => $estadisticas));
    }
    
    public function reindexarAction()
    {
        $defaultData = array('cantidad' => '20');
        $form = $this->createFormBuilder($defaultData)
            ->add('cantidad','text')
            ->getForm();
        if($this->getRequest()->getMethod() == "POST"){
            $form->bind($this->getRequest());
            $cant = $form->get('cantidad')->getData();
            
            $em = $this->getDoctrine()->getEntityManager();
            $users = $em->createQuery('SELECT u FROM AmistateBundle:Usuario u WHERE u.reindexar = 1')->setMaxResults($cant)->getResult();
            foreach($users as $user){
                $user->setReindexar(0);
                $em->persist($user);
                
                $this->get('solr_manager')->checkAndProcessUser($user);
            }
            $em->flush();
        }
        return $this->render('AdminBundle:Default:reindexar.html.twig',array(
            'form' => $form->createView(),
            ));
    }
}
